package com.dbh.company.util;

import cn.hutool.core.date.DateUtil;
import org.springframework.expression.Expression;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Date;

/**
 * @author dongbanghui
 * @create 2023/2/24
 */
public class SpelUtil {

    /**
     * 解析spEL表达式
     * @param result 对象
     * @param descriptionExpression spEL表达式
     * @return 结果
     */
    public static String parseDescriptionExpression(Object result, String descriptionExpression) {
        SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
        Expression expression = spelExpressionParser.parseExpression(descriptionExpression, new TemplateParserContext());
        String value = expression.getValue(new StandardEvaluationContext(result), String.class);
        if (value!= null && value.contains(",")) {
            value = value.replace(",", " ");
        }
        return value;
    }

    public static void main(String[] args) {
        TransferContractSignQuery transferContractSignQuery = new TransferContractSignQuery();
        transferContractSignQuery.setStatus(1);
        transferContractSignQuery.setStartCreateTime(DateUtil.formatDateTime(new Date()));
        String s = parseDescriptionExpression(transferContractSignQuery, "#{status?:''},#{startCreateTime}");
        System.out.println(s);
    }
}
